<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  
  <title>
    BLE 帧格式详解 | 朝雨绫萧的博客
  </title>
  <meta content="width=device-width, initial-scale=1" name="viewport">
  <meta name="theme-color" content="#4184f3">
  
  
  <link href="/image/favicon.png" rel="icon" />
  

  
<link rel="stylesheet" href="/css/highlight.light.css">

  
<link rel="stylesheet" href="/css/prism-customize.css">

  
<link rel="stylesheet" href="/css/nav-icon.css">

  
<link rel="stylesheet" href="/css/waves.min.css">

  
<link rel="stylesheet" href="/css/jquery.tocify.css">

  
<link rel="stylesheet" href="/css/main.css">

  
<link rel="stylesheet" href="/css/nav-indicator.css">

  
<link rel="stylesheet" href="/css/twikoo.css">

  
<link rel="stylesheet" href="/css/search.css">

  
  

  

  </meta>
  </meta>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  
<script src="/js/comment.js"></script>

  
<script src="/js/copy.js"></script>

<meta name="generator" content="Hexo 5.4.0"></head>

	<body>
		<header>
			<!-- cover image or sth. -->
		</header>
		<div id="main" class="m-scene">
			
<div class="nav-wrapper" id="header">

    <div class="container">
        <nav>
            
            
            <div class="logo wave">
                <a href="/" id="logo">
                    朝雨绫萧的博客
                </a>
            </div>

            

            <div class="nav-toggle-icon" >
                <div class="material-hamburger">
                    <span>
                    </span>
                    <span>
                    </span>
                    <span>
                    </span>
                </div>
            </div>

            <div class="menu-wrapper">
                <div class="nav-indicator">
                </div>
                <ul class="menus">
                    
                     
                        <li>
                            <a class="wave " href="/">
                                首页
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/archives">
                                归档
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/about">
                                关于
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/message">
                                留言
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/search">
                                搜索
                            </a>
                        </li>
                     
                    
                   
                </ul>
            </div>

        </nav>
    </div>
</div>


				<div class="container content">
					<div class="scene_element scene_element--fadein">
						<div class="row">
  <div class="main main-article">
    <article>

      
        <header class="post-header no-cover">
      
        </header>

      <!-- 标题 -->
      <h1 class="post-title">
        BLE 帧格式详解
      </h1>

      <section class="post-info post-info-bottom">

        <!-- 发布时间 -->
        <span class="post-date">
          <span class="post-date-text">
            发布于: 2021-02-27
          </span>
        </span>
        
        <!-- 分类 -->
        
          <span class="post-category">分类:
            <a class="article-category-link" href="/categories/BLE/">BLE</a>
          </span>
        

        <!-- 标签 -->
        
          <span class="post-tags">
            <ul class="post-tag-list" itemprop="keywords"><li class="post-tag-list-item"><a class="post-tag-list-link" href="/tags/BLE/" rel="tag">BLE</a></li></ul>
          </span>
        

        <!-- 阅读量统计 -->
        

      </section>

      <!-- 正文 -->
      <section class="post-content">
        <h1 id="BLE-帧格式详解"><a href="#BLE-帧格式详解" class="headerlink" title="BLE 帧格式详解"></a>BLE 帧格式详解</h1><h2 id="BLE-报文格式"><a href="#BLE-报文格式" class="headerlink" title="BLE 报文格式"></a>BLE 报文格式</h2><p><strong>BLE 报文格式</strong>（广播报文和数据报文通用）</p>
<p>报头加长度加数据合称 PDU（Packet Data Unit）。</p>
<p>报头加长度合称 PDU Header。</p>
<table>
<thead>
<tr>
<th align="center">1 byte</th>
<th align="center">4 byte</th>
<th align="center">1 byte</th>
<th align="center">1 byte</th>
<th align="center">0~37 byte</th>
<th align="center">3 byte</th>
</tr>
</thead>
<tbody><tr>
<td align="center">前导码</td>
<td align="center">接入地址</td>
<td align="center">报头</td>
<td align="center">长度</td>
<td align="center">数据</td>
<td align="center">CRC</td>
</tr>
</tbody></table>
<h3 id="广播报文格式"><a href="#广播报文格式" class="headerlink" title="广播报文格式"></a>广播报文格式</h3><p><strong>非定向广播包</strong></p>
<p>PDU MAX ：2 + 6 + 31 = 39 byte</p>
<escape>

<table>
<thead>
  <tr>
    <th colspan="11">10~47 byte</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>Preamble</td>
    <td>Access Address</td>
    <td>PDU type</td>
    <td>RFU</td>
    <td>TxAdd</td>
    <td>RxAdd</td>
    <td>length</td>
    <td>RFU</td>
    <td>AdvA</td>
    <td>data</td>
    <td>CRC</td>
  </tr>
  <tr>
    <td>1 byte</td>
    <td>4 byte</td>
    <td>4 bit</td>
    <td>2 bit</td>
    <td>1 bit</td>
    <td>1 bit</td>
    <td>6 bit</td>
    <td>2 bit</td>
    <td>6 byte</td>
    <td>0~31 byte</td>
    <td>3 byte</td>
  </tr>
</tbody>
</table>
</escape>

<p><strong>定向广播包</strong></p>
<escape>

<table>
<thead>
  <tr>
    <th colspan="11">22 byte</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>Preamble</td>
    <td>Access Address</td>
    <td>PDU type</td>
    <td>RFU</td>
    <td>TxAdd</td>
    <td>RxAdd</td>
    <td>length</td>
    <td>RFU</td>
    <td>AdvA</td>
    <td>InitA</td>
    <td>CRC</td>
  </tr>
  <tr>
    <td>1 byte</td>
    <td>4 byte</td>
    <td>4 bit</td>
    <td>2 bit</td>
    <td>1 bit</td>
    <td>1 bit</td>
    <td>6 bit</td>
    <td>2 bit</td>
    <td>6 byte</td>
    <td>6 byte</td>
    <td>3 byte</td>
  </tr>
</tbody>
</table>

</escape>

<h3 id="数据报文格式"><a href="#数据报文格式" class="headerlink" title="数据报文格式"></a>数据报文格式</h3><p><strong>LL DATA 格式</strong></p>
<p>PDU MAX ：2 + 6 + 31 = 39 byte</p>
<escape>

<table>
<thead>
  <tr>
    <th colspan="16">10~41 byte</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>Preamble</td>
    <td>Access Address</td>
    <td>LLID</td>
    <td>NESN</td>
    <td>SN</td>
    <td>MD</td>
    <td>RFU</td>
    <td>length</td>
    <td>RFU</td>
    <td>length</td>
    <td>ChannelID</td>
    <td>attribute opcode</td>
    <td>attribute handle</td>
    <td>attribute value</td>
    <td>mic</td>
    <td>crc</td>
  </tr>
  <tr>
    <td>1 byte</td>
    <td>4 byte</td>
    <td>2 bit</td>
    <td>1 bit</td>
    <td>1 bit</td>
    <td>1 bit</td>
    <td>3 bit</td>
    <td>5 bit</td>
    <td>3 bit</td>
    <td>2 byte</td>
    <td>2 byte</td>
    <td>1 byte</td>
    <td>2 byte</td>
    <td>0~(MTU -3) byte</td>
    <td>4 byte</td>
    <td>3 byte</td>
  </tr>
</tbody>
</table>
</escape>

<p><strong>LL Control 格式</strong></p>
<escape>

<table>
<thead>
  <tr>
    <th colspan="13">11~41 byte</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>Preamble</td>
    <td>Access Address</td>
    <td>LLID</td>
    <td>NESN</td>
    <td>SN</td>
    <td>MD</td>
    <td>RFU</td>
    <td>length</td>
    <td>RFU</td>
    <td>opcode</td>
    <td>Ctrdata</td>
    <td>mic</td>
    <td>crc</td>
  </tr>
  <tr>
    <td>1 byte</td>
    <td>4 byte</td>
    <td>2 bit</td>
    <td>1 bit</td>
    <td>1 bit</td>
    <td>1 bit</td>
    <td>3 bit</td>
    <td>5 bit</td>
    <td>3 bit</td>
    <td>1 byte</td>
    <td>0~26 byte</td>
    <td>4 byte</td>
    <td>3 byte</td>
  </tr>
</tbody>
</table>
</escape>


<h2 id="前导码"><a href="#前导码" class="headerlink" title="前导码"></a>前导码</h2><p>前导码为 10101010 or 01010101，接收机可以利用前导码来配置自动增益，确定频率。前导码由接入地址的第一个比特决定，第一个比特为 0，则前导码使用 01010101，反之，则用10101010。</p>
<h2 id="接入地址"><a href="#接入地址" class="headerlink" title="接入地址"></a>接入地址</h2><p>为了防止噪声伪造的前导码，设置了接入地址，用来区分是噪声还是报文。接入地址分为广播接入地址和数据接入地址。广播接入地址为固定值：0x8E89BED6。数据接入地址为随机值。</p>
<h2 id="报头"><a href="#报头" class="headerlink" title="报头"></a>报头</h2><p>报头包含了广播报文的类型和标记位。</p>
<h3 id="广播报头"><a href="#广播报头" class="headerlink" title="广播报头"></a>广播报头</h3><p>广播报头包含<a href="#%E5%B9%BF%E6%92%AD%E6%8A%A5%E6%96%87%E6%A0%BC%E5%BC%8F">广播报文类型，保留位，发送地址类型和接收地址类型</a>：</p>
<p>广播数据类型(PDU type）有 7 种，有不同的净荷格式和行为：</p>
<ul>
<li>ADV_IND - 通用广播<ul>
<li>可以被扫描，也可以被连接</li>
</ul>
</li>
<li>ADV_DIRECT_IND - 定向连接<ul>
<li>针对指定设备进行快速连接</li>
</ul>
</li>
<li>ADV_NONCONN_IND - 不可连接<ul>
<li>只广播，不可被扫描或连接</li>
</ul>
</li>
<li>ADV_SCAN_IND - 可扫描<ul>
<li>可以被扫描，不可以被连接</li>
</ul>
</li>
<li>SCAN_REQ - 主动扫描请求</li>
<li>SCAN_RSP - 主动扫描响应</li>
<li>CONNECT_REQ - 连接请求</li>
</ul>
<p>发送地址类型（TxAdd）决定广播设备地址（AdvA）的值类型。TxAdd 为 1，AdvA 为 advertiser 的 Public Address，TxAdd 为 0，AdvA 为 Random Address。</p>
<p>接收地址类型（RxAdd）在定向广播中使用，决定了需要快速连接设备的地址类型。1 为 Public Address，0 为 Random Address。</p>
<blockquote>
<p>公共地址由制造商从IEEE申请，由IEEE注册机构为该制造商分配的机构唯一标识符OUI(Organizationally Unique Identifier)。这个地址是独一无二，不能修改</p>
<p>随机地址包含两种：静态地址（Static Device Address）和私有地址（PrivateDevice Address）。</p>
</blockquote>
<h3 id="数据报头"><a href="#数据报头" class="headerlink" title="数据报头"></a>数据报头</h3><p>数据报头包含<a href="#%E6%95%B0%E6%8D%AE%E6%8A%A5%E6%96%87%E6%A0%BC%E5%BC%8F">链路层标识符，下一报文序号，序号，更多数据和保留位</a>：</p>
<p>链路层标识符（LLID）：</p>
<ul>
<li>00 ：保留</li>
<li>01 ：LL Data PDU<ul>
<li>剩余的L2CAP 包或者空包</li>
</ul>
</li>
<li>10 ：LL Data PDU<ul>
<li>L2CAP 包的开始或者完整的L2CAP 包</li>
</ul>
</li>
<li>11 ：LL Control PDU</li>
</ul>
<p>下一报文序号（NESN）：期望的下一个包的序号</p>
<p>序号（SN）：当前包的序号</p>
<p>SN 和 NESN 一起完成对上一个数据包的 ack 和重传功能。</p>
<p>更多数据（MD），more data，用来指示对方我还有数据包要传，请继续打开射频窗口准备接收。该位实现了一个 connection interval 进行多次数据包传输。</p>
<h2 id="长度"><a href="#长度" class="headerlink" title="长度"></a>长度</h2><p>广播报文的长度位有 6bit，数据报文的长度位只有 5bit，所以 PDU 的数据区域，广播报文最大可以有37个数据，而数据报文最大只有31个数据。</p>
<h2 id="Payload"><a href="#Payload" class="headerlink" title="Payload"></a>Payload</h2><h3 id="广播类-Payload"><a href="#广播类-Payload" class="headerlink" title="广播类 Payload"></a>广播类 Payload</h3><p>数据净荷需要更具 PDU type 确定，不同的 type，净荷都不一样：</p>
<ul>
<li>ADV_IND</li>
<li>ADV_NONCONN_IND </li>
<li>ADV_SCAN_IND <ul>
<li>AdvA（6 byte）+ AdvData（0~31 byte）</li>
</ul>
</li>
<li>ADV_DIRECT_IND  <ul>
<li>AdvA（6 byte）+ TargetA（6 byte）</li>
</ul>
</li>
<li>SCAN_REQ <ul>
<li>ScanA（6 byte）+ AdvA（6 byte）</li>
</ul>
</li>
<li>SCAN_RSP <ul>
<li>AdvA（6 byte）+ ScanRspData（0~31 byte）</li>
</ul>
</li>
<li>CONNECT_REQ <ul>
<li>InitA（6 Bytes）+ AdvA（6bytes）+LLData（22 Bytes）</li>
</ul>
</li>
</ul>
<h3 id="数据类-Payload"><a href="#数据类-Payload" class="headerlink" title="数据类 Payload"></a>数据类 Payload</h3><h4 id="控制数据包"><a href="#控制数据包" class="headerlink" title="控制数据包"></a>控制数据包</h4><p>控制类数据包由 Opcode（1 byte） + Ctrdata（0~26 byte）组成。Ctrdata 由 Opcode 决定。</p>
<table>
<thead>
<tr>
<th>操作码</th>
<th>控制帧名称</th>
<th>解释</th>
</tr>
</thead>
<tbody><tr>
<td>0x00</td>
<td>LL_CONNECTION_UPDATE_REQ</td>
<td>更新连接参数</td>
</tr>
<tr>
<td>0x01</td>
<td>LL_CHANNEL_MAP_REQ</td>
<td>设置跳频范围（37、38、39保留）</td>
</tr>
<tr>
<td>0x02</td>
<td>LL_TERMINATE_IND</td>
<td>指示连接中断的原因</td>
</tr>
<tr>
<td>0x03</td>
<td>LL_ENC_REQ</td>
<td>加密请求</td>
</tr>
<tr>
<td>0x04</td>
<td>LL_ENC_RSP</td>
<td>加密回复</td>
</tr>
<tr>
<td>0x05</td>
<td>LL_START_ENC_REQ</td>
<td>加密请求</td>
</tr>
<tr>
<td>0x06</td>
<td>LL_START_ENC_RSP</td>
<td>加密回复</td>
</tr>
<tr>
<td>0x07</td>
<td>LL_UNKNOWN_RSP</td>
<td>未知操作码指示</td>
</tr>
<tr>
<td>0x08</td>
<td>LL_FEATURE_REQ</td>
<td>请求支持的特性</td>
</tr>
<tr>
<td>0x09</td>
<td>LL_FEATURE_RSP</td>
<td>回复支持的特性</td>
</tr>
<tr>
<td>0x0A</td>
<td>LL_PAUSE_ENC_REQ</td>
<td>暂停加密请求</td>
</tr>
<tr>
<td>0x0B</td>
<td>LL_PAUSE_ENC_RSP</td>
<td>暂停加密回复</td>
</tr>
<tr>
<td>0x0C</td>
<td>LL_VERSION_IND</td>
<td>指示 controller 版本和公司信息</td>
</tr>
<tr>
<td>0x0D</td>
<td>LL_REJECT_IND</td>
<td>拒绝控制命令指示</td>
</tr>
<tr>
<td>0x0E</td>
<td>LL_SLAVE_FEATURE_REQ</td>
<td>NA</td>
</tr>
<tr>
<td>0x0F</td>
<td>LL_CONNECTION_PARAM_REQ</td>
<td>连接参数请求</td>
</tr>
<tr>
<td>0x10</td>
<td>LL_CONNECTION_PARAM_RSP</td>
<td>连接参数回复</td>
</tr>
<tr>
<td>0x11</td>
<td>LL_REJECT_IND_EXT</td>
<td>扩展拒绝控制命令指示</td>
</tr>
<tr>
<td>0x12</td>
<td>LL_PING_REQ</td>
<td>握手包</td>
</tr>
<tr>
<td>0x13</td>
<td>LL_PING_RSP</td>
<td>握手包</td>
</tr>
<tr>
<td>0x14</td>
<td>LL_LENGTH_REQ</td>
<td>请求发送接收的字节和交互时间（27<del>251bytes、328</del>2120ms）</td>
</tr>
<tr>
<td>0x15</td>
<td>LL_LENGTH_RSP</td>
<td>回应发送接收的字节和交互时间</td>
</tr>
<tr>
<td>0x16~FF</td>
<td>Reserved for Future Use</td>
<td>保留未来使用</td>
</tr>
</tbody></table>
<h2 id="CRC"><a href="#CRC" class="headerlink" title="CRC"></a>CRC</h2><p>BLE 采用的是 24 位 CRC 校验。CRC 对报头、长度和数据进行计算。</p>

      </section>

    </article>
    
    


    <!-- 版权声明 -->
    <div class="post-copyright">
	<div>
		<strong>本文作者：</strong>
		<span>
			朝雨绫萧
		</span>
	</div>
	<div>
		<strong>本文链接：</strong>
		<a href="http://zzylxx.gitee.io/帧格式详解">
			http://zzylxx.gitee.io/帧格式详解
		</a>
	</div>
	<div>
		<span>欢迎任何形式的转载，但请务必注明出处。</span>
	</div>
	<div>
		<span>由于笔者水平有限，如果文章或代码有表述不当之处，还请不吝赐教。</span>
	</div>
</div>


<style>
	.post-copyright {
		background-color: #fff;
		/* box-shadow: 0px 0px 1px 1px rgba(0, 0, 0, 0.05); */
		box-shadow: 0 6px 30px -10px rgba(0, 0, 0, 0.1);
		display: block;
		margin: 20px auto;
		margin-bottom: 15px;
		padding: 10px;
		font-size: 15px;
		color: #2e2d2d;
		word-break: break-all;
		line-height: 1.8;
	}


	.post-copyright a {
		color: #2e2d2d;
		border-bottom: 1px solid #999;
	}

	.post-copyright a:hover {
		color: #2e2d2d;
		border-bottom: 1px solid #000;
	}
</style>

    <!-- comments -->
    <div class="comments">
    <h3 id="commenttitle" style="margin-top: 20px;margin-left: 10px;">评论区</h3>
    <div id="tcomment"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/twikoo@1.0.0/dist/twikoo.all.min.js"></script>
<script>twikoo.init({ envId: 'twikoo-1gx61ki809c44dc7', el: '#tcomment' })</script>

    <div class="right-icon">
      <div id="to-valine" class="right-icon-item">
        <a href="#commenttitle">评论</a>
      </div>
    </div>

  </div>


  <aside>
    <div class="aside-wrap">
    
        <section class="widget-wrap">
    <h3 class="widget-title">简介</h3>
    <div class="widget">
        <div class="notice">
            <img class="notice-img" src="/image/head_portrait.jpg">
            <p>路漫漫其修远兮，<br>吾将上下而求索!<br></p>
            
            <p class="notice-item">
                <!-- github -->
                <span><a target="_blank" href="https://github.com/qgyhd1234"><img src="https://shuxhan-imgbed.oss-cn-hangzhou.aliyuncs.com/img/20210108165043.jpg" alt="Github"></a></span>
                <!-- juejin -->
                <!-- <span><a target="_blank" href=""><img src="https://shuxhan-imgbed.oss-cn-hangzhou.aliyuncs.com/img/20210108165800.png" alt="掘金社区"></a></span> -->
                <!-- zhihu -->
                <!-- <span><a target="_blank" href=""><img src="https://shuxhan-imgbed.oss-cn-hangzhou.aliyuncs.com/img/20210108172745.ico" alt="知乎"></a></span> -->
                <!-- csdn -->
                <!-- <span><a target="_blank" href=""><img src="https://shuxhan-imgbed.oss-cn-hangzhou.aliyuncs.com/img/20210108172932.ico" alt="CSDN"></a></span> -->
            </p>

            <ul class="statistics">
                <li>
                    <span>文章</span>
                    <a href="./archives">7</a>
                </li>
                <li>
                    <span>字数</span>
                    <a>9.7k</a>
                </li>
                <li>
                    <span>访客</span>
                    <a id="busuanzi_value_site_uv"></a>
                </li>
                
                
            </ul>

            <div id="follow" class="button">
                <a target="_blank" href="https://github.com/shuxhan">关注</a>
            </div>
        </div>
    </div>
</section>

    
        
  <section class="widget-wrap">
    <h3 class="widget-title">文章分类</h3>
    <div class="widget">
      <div class="categories-list">
        
           
            
              <a class="category-list-item" href="/categories/BLE">
                <span class="category-list-link">BLE</span>
                <span class="category-list-number">(7)</span>
              </a>
            
          
        
      </div>
    </div>
  </section>


    
        
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/BLE/" rel="tag">BLE</a></li></ul>
    </div>
  </div>


    
        
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul class="recent-posts">
        
          <li>
            <a href="/2021/02/28/GAP%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE/">BLE GAP参数设置</a>
          </li>
        
          <li>
            <a href="/2021/02/28/BLE%E5%B9%BF%E6%92%AD%E5%92%8C%E6%89%AB%E6%8F%8F%E5%93%8D%E5%BA%94/">BLE 广播和扫描响应</a>
          </li>
        
          <li>
            <a href="/2021/02/28/BLE%20%E8%BF%9E%E6%8E%A5/">BLE 连接</a>
          </li>
        
          <li>
            <a href="/2021/02/28/BLE%20SM/">BLE Security Manager</a>
          </li>
        
          <li>
            <a href="/2021/02/28/BLE%20GATT/">BLE GATT</a>
          </li>
        
      </ul>
    </div>
  </div>

    
        
  <section class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/02/">二月 2021</a></li></ul>
    </div>
  </section>


    
    
</div>


    <!-- 目录 -->
    
  </aside>
  
</div>

					</div>
				</div>

				<!-- 返回顶部按钮 -->
				<div class="right-icon">
					<div id="to-top" class="right-icon-item">
						<a>
							<span class="top-left"></span>
							<span class="top-right"></span>
						</a>
					</div>
				</div>
		</div>
		<footer class="footer">

    <div class="copyright-wrap">
      <span>&copy; 2021-2021 朝雨绫萧</span>
      <span id="timeDate">载入天数...</span><span id="busuanzi_container_site_pv">
      总访问量<span id="busuanzi_value_site_pv"></span>次
    </span>
    </div>
    
    
    <!-- <span id="busuanzi_container_site_uv">
      ，本站访客数<span id="busuanzi_value_site_uv"></span>人次
    </span> -->
    
</footer>

<script>
    var now = new Date();
    function createtime() {
      var grt = new Date("2/27/2021 16:12:00");//此处修改你的建站时间或者网站上线时间 
      now.setTime(now.getTime() + 250);
      days = (now - grt) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
      document.getElementById("timeDate").innerHTML = "本站已安全运行 " + dnum + " 天 ";
    }
    setInterval("createtime()", 1000);
</script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<script src="/lib/jquery.js"></script>


<script src="/lib/waves.js"></script>


<script src="/lib/jquery-ui.js"></script>


<script src="/lib/jquery.tocify.js"></script>


<script src="/js/main.js"></script>


			
<style>
  .copy-btn {
    display: inline-block;
    padding: 6px 12px;
    font-size: 13px;
    font-weight: 500;
    line-height: 20px;
    color: rgb(206, 206, 206);
    white-space: nowrap;
    vertical-align: middle;
    cursor: pointer;
    border-radius: 3px;
    user-select: none;
    outline: 0;
  }

  .copy-btn:hover {
    color: #888
  }

  .highlight-wrap .copy-btn {
    padding: 2px 6px;
    position: absolute;
    right: 10px;
    top: 1px;
    z-index: 1;
  }



  .highlight-wrap {
    position: relative;
  }

  #copysuccess {
    width: 200px;
    height: 75px;
    position: fixed;
    right: 50%;
    margin-right: -100px;
    top: 40%;
    margin-top: -40px;
    color: #fff;
    font-weight: 600;
    background: #444444d8;
    display: none;
    z-index: 99999;
    border-radius: 10px;
    text-align: center;
    line-height: 30px;
    padding-top: 10px;
    user-select: none;
  }
</style>

<script>
  addLoadEvent(() => {
    $('.highlight').each(function (i, e) {
      var $wrap = $('<div>').addClass('highlight-wrap')
      $(e).after($wrap)
      $wrap.append($('<div>').addClass('copy-btn').append('复制代码').on('click', function (e) {
        var copysuccess = document.getElementById('copysuccess');
        copysuccess.style.display = 'block'
        setTimeout(function () {
          copysuccess.style.display = 'none'
        }, 2000)
        var code = $(this).parent().find(".code")[0].innerText
          
                var ta = document.createElement('textarea')
        document.body.appendChild(ta)
        ta.style.position = 'absolute'
        ta.style.top = '0px'
        ta.style.left = '0px'
        ta.value = code
        ta.select()
        ta.focus()
        var result = document.execCommand('copy')
        document.body.removeChild(ta)
          
                  if (result) $(this).text('复制成功')
            else $(this).text('复制失败')
              
                $(this).blur()
      })).on('mouseleave', function (e) {
        var $b = $(this).find('.copy-btn')
        setTimeout(function () {
          $b.text('复制代码')
        }, 1000)
      }).append(e)
    })
  })
</script>

				<div id="copysuccess">复制成功<br>欢迎阅读文章~</div>
	</body>

</html>

<script>
	$('#to-top').click(function () { $('html,body').animate({ scrollTop: '0px' }, 500); return false; });
</script>